package com.sq.partner.manager.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
 * 密码工具类
 * @author 王雁
 * 时间 2015年5月4日17:27:12
 *
 */
public class PasswordUtil {

	private static final String LETTERCHAR = "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
	private static final String NUMBERCHAR = "123456789";
	private static final String SPECIALCHAR = "!@#$%^&*?";
	

	/**
	 * length表示生成字符串的长度
	 * @param length
	 * @param base
	 * @return
	 */
	private static String getRandomString(int length,String base) { 
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString();   
	}
	/**
	 * 打乱数据，将生产的密码重新排列
	 * @param indexString
	 * @return
	 */
	private static String disruptedData(String indexString) {
		char [] strArr = indexString.toCharArray();
		List<String> dataArray = new ArrayList<String>();
		for (char c : strArr) {
			dataArray.add(String.valueOf(c));
		}
		String resultdata[] = new String[indexString.length()];
		int i = 0;
		while (dataArray.size()>0) {
			int index = (int) (Math.random() * dataArray.size());
			resultdata[i] = dataArray.get(index);
			i++;
			dataArray.remove(index);
		}
		StringBuffer sb = new StringBuffer();
		for (int m = 0; m < resultdata.length; m++) {
			sb.append(resultdata[m]);
		}
		return sb.toString();
	}

	/**
	 * 获取生产的密码
	 * @return
	 */
	public static String getPassword(){
		String indexPwd = getRandomString(5,LETTERCHAR)+getRandomString(2,NUMBERCHAR)+getRandomString(1,SPECIALCHAR);
		return disruptedData(indexPwd);
	}
	
	/**
	 * 校验密码复杂度
	 * @param password
	 * @return
	 */
	public static boolean checkPassword(String password){
		String reg = "^((?=.*[0-9])(?=.*[a-zA-Z])(?=.*[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u00FF]).{8,})";//包含数字、字母、特殊字符最少8位
		return password.matches(reg);
	}
	public static void main(String[] args) {
		String indexPwd = getRandomString(5,LETTERCHAR)+getRandomString(2,NUMBERCHAR)+getRandomString(1,SPECIALCHAR);
		System.out.println(indexPwd);
		String endPwd = disruptedData(indexPwd);
		System.out.println(checkPassword("111s11~111"));
	}
	
	
}
